<?php


namespace app\dao\query;

session_start();
use app\model\RoomPassword;

class RoomPasswordQueryDao
{
    private $query;

    public function __construct()
    {
        $this->query = new RoomPassword();
    }

    /**
     * 获取列表
     * @param int $page -
     * @param int $limit -
     * @return \think\Paginator -
     */
    public function getList(int $page = 1, int $limit = 10)
    {
        return $this->query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
    }

    /**
     * 获取所有数据
     * @return array -
     */
    public function getAll()
    {
        return $this->query->select();
    }

    /**
     * 获取一个
     * @return mixed
     */
    public function getOne()
    {
        return $this->query->find();
    }


    /**
     * 获取当前筛选下的总记录数
     * @return int -
     */
    public function getCount()
    {
        return $this->query->count();
    }

    public function orderByRoomId($sort = 'asc'){
        $this->query = $this->query->order('room_id' , $sort);
        return $this;
    }

    public function withRoom(){
        $this->query = $this->query->with(['Room' => function($query){
            return $query->with('roomType');
        }]);
        return $this;
    }

    public function remove(){
        $this->query = $this->query->removeOption();
        return $this;
    }

    public function filterByRoomId($room_id){
        if ($room_id != null)$this->query = $this->query->where('room_id' , $room_id);
        return $this;
    }

    public function filterById($id){
        if ($id != null)$this->query = $this->query->where('id' , $id);
        return $this;
    }
    // Todo ：废弃
    public function StartTimeAndEndTimeByTime($start_time , $end_time){
        $this->query = $this->query->where([
            ['end_time' , '>=' , $start_time],
            ['start_time' , '<=' , $end_time]
        ]);
        return $this;
    }

    public function StartBetweenEndTime($time , $start_time , $end_time){
        $this->query = $this->query->where([
            ['start_time' , '<=' ,  $time],
            ['end_time' , '>=' , $time],
            ['end_time' , '>=' , $start_time],
            ['start_time' , '<=' , $end_time],
        ]);
        return $this;
    }
}